﻿@model ProviderModelList
@using CAF.WebSite.Application.WebUI.Mvc;
@using CAF.Infrastructure.Core;
@helper ActiveLabel(ProviderModel item) 
{ 
	var activatable = item as IActivatable;
	if (activatable == null)
	{
		return;
	}
	if (activatable.IsActive)
	{
		<span class="label label-success">@T("Common.Active")</span>
	}
	else
	{
		<span class="label label">@T("Common.Inactive")</span>
	}
}

@functions {
	private object RenderTemplate(object template)
	{
		var result = template as HelperResult;
		if (result != null)
		{
			result.WriteTo(Html.ViewContext.Writer);
		}
		return null;
	}

	private bool IsActive(ProviderModel item)
	{
		var activatable = item as IActivatable;
		if (activatable == null)
		{
			return true;
		}

		return activatable.IsActive;
	}
	
	private string GetColumnWidth()
	{
		return "{0}%".FormatInvariant(100 / (Model.ExtraColumns.Count + 1));
	}
}

<div class="module-list@(Model.Data.Any(x => x.IsEditable) ? " sortable-list" : "")">
	@foreach (var item in Model.Data)
	{
		<div class='module-item sortable-item clearfix@(!IsActive(item) ? " inactive" : "")' data-system-name="@item.SystemName" data-friendly-name="@item.FriendlyName">
			@if (item.IsEditable)
			{
				<div class="sortable-grip"></div>
			}
			<div class="module-icon">
				<img class="icon img-responsive" src="@Url.Content(item.IconUrl)" />
			</div>
			<div class="module-data module-data-cols-@(Model.ExtraColumns.Count + 1) clearfix">
				<!-- General -->
				<div class="module-col">
					<div>
						<span class="module-name">@item.FriendlyName</span>
						@ActiveLabel(item)
					</div>
					<div class="module-info">
						@{
							string tip = item.ProvidingPluginFriendlyName.HasValue() ? T("Admin.Providers.ProvidingPlugin") + ": " + item.ProvidingPluginFriendlyName : "";
						}
						<span class="attr-name">@T("Common.SystemName"):</span>
						<span class="attr-value" @Html.Attr("title", tip, tip.HasValue())>@item.SystemName</span>

					</div>
					@if (item.Description.HasValue())
					{
						<div class="module-description">
							@item.Description
						</div>					
					}
					@if (item.IsEditable || item.IsConfigurable)
					{
						<div class="module-actions">
							<div class="btn-group">
								@if (item.IsEditable)
								{
									<a class="btn btn-default btn-small module-edit" href='@Url.Action("EditProviderPopup", "Plugin", new { systemName = item.SystemName, btnId = "btnRefresh" })'>
										<i class="fa fa-pencil-square-o"></i>
										@T("Admin.Common.Edit")
									</a>
								}
								@if (item.IsConfigurable)
								{
                                    <a class="btn btn-default btn-small module-configure" href="@Url.Action("ConfigureProvider", "Plugin", new { systemName = item.SystemName })">
                                        <i class="fa fa-cog"></i>
                                        @T("Admin.Common.Configure")
                                    </a>
								}
							</div>
						</div>						
					}
				</div>
				@foreach (var col in Model.ExtraColumns)
				{
					<div class="module-col">
						@RenderTemplate(col(item))
					</div>
				}
			</div>
		</div>
	}
</div>

<button type="button" id="btnRefresh" class="hide"></button>

<script>
	$(function () {
		$('.module-list').on('click', '.module-edit', function (e) {
			e.preventDefault();
			OpenWindow($(this).attr("href"), 800, 250, true);
			return false;
		});

		$('#btnRefresh').on('click', function () {
			// reload page
			setLocation(location.href);
			return false;
		});
		
		$('.sortable-list').sortable({
			items: '.module-item',
			forcePlaceholderSize: true,
			handle: '.sortable-grip'
		}).on('sortupdate', function (e, ui) {
			var items = $(this).closest('.sortable-list').find('.module-item');
			var newOrder = [];
			$.each(items, function (i, val) {
				newOrder.push($(val).data('system-name'));
			});
			$.ajax({
				async: false,
				cache: false,
				type: 'POST',
				//dataType: 'json',
				url: '@Url.Action("SortProviders", "Plugin")',
				data: { providers: newOrder.join(",") }
			});
		});
	});
</script>


